let catFalg = false;

// import {HOME} from './constConfig'

$(function () {

    $('#login_btn').on('click', function () {
        let change_img = document.querySelector('#change_img')
        //判断验证码是否成功 成功则进入登陆判断函数
        captchaValid()
        if (catFalg) {
            login(change_img)
        } else {
            changecaptchaValid(change_img);
        }
    })
})

function login(change_img) {
    let chageFalg = false
    future.ajax({
        url: '/users/login',
        type: 'post',
        data: {
            name: $('#login_name').val(),
            pwd: $('#login_pwd').val(),
        },
        success: function (result) {
            console.log(result.username)
            chageFalg = true;
            window.localStorage.setItem("token", result.jwt_token);
            alert("登录成功,跳转至主界面")
            window.location.href = "../NewPage/html/New_index.html"
            // window.location.href = "./oldHome.html"
            // window.location.href = "./"+HOME
            console.log(HOME)
        }, error: function () {
        }
    })
    if (!chageFalg) {
        alert("账户或者密码错误")
        changecaptchaValid(change_img);
    }

}

function captchaValid() {
    future.ajax({
        url: '/users/captchaValid',
        type: 'get',
        data: {
            captcha: $('#capText').val()
        }, success: function (result) {
            catFalg = true;
            console.log("验证码正确")
        }, error: function (err) {
            catFalg = false;
            alert("验证码输入错误")
        }
    })
}

function changecaptchaValid(obj) {
    console.log(obj)
    obj.setAttribute('src', 'http://localhost:3000/users/getCaptcha?v=' + new Date().getTime())
}